package com.coollang.smartbra.activity;

import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.CountActionDetailsAdapter;
import com.coollang.smartbra.beans.ActionData;
import com.coollang.smartbra.fragment.MyDialogFragment;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CountActionDetailsActivity extends MyAutoLayoutActivity {

    private String TAG = "CountA";
    @BindView(R.id.status_bar)
    View mStatusBar;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_second)
    TextView mTvSecond;
    @BindView(R.id.tv_error_action_count)
    TextView mTvErrorActionCount;
    @BindView(R.id.pb_progressbar)
    ProgressBar mProgressBar;
    @BindView(R.id.tv_correct_proportion)
    TextView mTvCorrectProportion;
    @BindView(R.id.tv_error_proportion)
    TextView mTvErrorProportion;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.iv_back)
    ImageView mIvBack;

    private ArrayList<ActionData> mActionDataList;
    private CountActionDetailsAdapter mActionDetailsAdapter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_count_action_details);
        ButterKnife.bind(this);
        // 设置沉浸式状态栏
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(mStatusBar, this, content);
        initViews();
    }

    /**
     * 初始化 UI 相关内容
     */
    private void initViews() {
        setBatteryCount(2 + " 个");

        mProgressBar.setProgress(96);
        // 设置二级
        mProgressBar.setSecondaryProgress(100);
        mProgressBar.setBackgroundColor(0x00000000);

        mActionDetailsAdapter = new CountActionDetailsAdapter(this);
        mRecyclerView.setAdapter(mActionDetailsAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });
    }

    private int correctCount;

    @Override
    protected void initData() {
        super.initData();
        mActionDataList = getIntent().getParcelableArrayListExtra("list");
        Collections.reverse(mActionDataList);
        mActionDetailsAdapter.addData(mActionDataList);

        for (ActionData actionData : mActionDataList) {
            if (actionData.getErrorFlag() == 1) {
                // 正确个数
                correctCount++;
            }
        }

        // 设置正确个数
        mTvSecond.setText(correctCount + " 个");
        int size = mActionDataList.size();
        mTvErrorActionCount.setText("不规范动作 " + (size - correctCount) + " 个");

        // 正确的比例
        int correctProportion = (int) ((float) correctCount / size * 100);
        mTvCorrectProportion.setText(correctProportion + "%");
        mTvErrorProportion.setText((100 - correctProportion) + "%");

        // 设置进度条
        // 设置progressbar
        mProgressBar.setProgress(correctProportion);
        // 设置二级
        mProgressBar.setSecondaryProgress(100);
        mProgressBar.setBackgroundColor(0x00000000);
    }

    private void setBatteryCount(String text) {
        // 第一行
        SpannableString msp = new SpannableString(text);
        msp.setSpan(new RelativeSizeSpan(2.6f), 0, text.length() - 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTvSecond.setText(msp);
    }

    private ActionData mClickActionData;

    public void onClickErrorItem(int position) {
        // 点击了错误的条目
        mClickActionData = mActionDetailsAdapter.getItem(position);
        showDialog();
    }

    public String getPlayVideoPath() {
        return "android.resource://" + getPackageName() + "/" + mClickActionData.getErrorVideo();
    }

    private void showDialog() {
        MyDialogFragment dialogFragment = new MyDialogFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        dialogFragment.show(fragmentManager, "MyDialogFragment");

        dialogFragment.setOnDialogListener(new MyDialogFragment.OnDialogListener() {
            @Override
            public int onClickNext() {
                // 点击了下一首
                int position = mActionDetailsAdapter.getItemPosition(mClickActionData);

                if (position == mActionDetailsAdapter.getItemCount() - 1) {
                    return 0;
                } else {
                    // 获取上一条错误视频
                    mClickActionData = mActionDetailsAdapter.getItem(++position);
                    return 1;
                }
            }

            @Override
            public String getVideoPath() {
                return getPlayVideoPath();
            }

            @Override
            public int onClickLast() {
                int position = mActionDetailsAdapter.getItemPosition(mClickActionData);

                if (position == 0) {
                    return 0;
                } else {
                    // 获取下一条错误视频
                    mClickActionData = mActionDetailsAdapter.getItem(--position);
                    return 1;
                }
            }
        });
    }
}
